const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');
const util = require('../../utils/util');

Page({
  data: {
    colorTheme: app.globalData.colorTheme,
    colorBackground: app.globalData.colorBackground,
    activityList: [],
    pageNum: 1,
    pageSize: 5,
    params: {},
    hotActivityList: [],
    newActivityList: [],
    swiperList: [],
    activityList: [],
    isCard: false // 列表每项样式类型
  },

  /**
   * 获取活动列表
   */
  async getActivityList(pageNum, pageSize, params){
    let token = wx.getStorageSync("token");
    let url = config.SERVER_URL + 'activity/list/activities?pageNum=' + pageNum + '&pageSize=' + pageSize;
    wx.showLoading({
      title: '加载ing'
    });
    await request.requestGetApi(url, token, params, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        res.data.rows.forEach(item => {
          item.actStatus = util.computedTimeStatus(item.startTime, item.endTime);
        });
        if(params.order === 'create_time'){
          this.setData({
            newActivityList: res.data.rows,
            swiperList: this.data.swiperList.concat(res.data.rows.slice(0, 2))
          });
        }else if(params.order === 'appointment_count'){
          this.setData({
            hotActivityList: res.data.rows,
            swiperList: this.data.swiperList.concat(res.data.rows.slice(0, 2))
          });
        }else{
          this.setData({
            activityList: res.data.rows
          });
        }
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getActivityList(this.data.pageNum, this.data.pageSize, { order:'create_time' });
    this.getActivityList(this.data.pageNum, this.data.pageSize, { order:'appointment_count' });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 跳转到寻找队友列表页面
   */ 
  toUserList(){
    wx.navigateTo({
      url: '/pages/user-list/user-list'
    });
  },

  /**
   * 跳转到寻找队伍列表页面
   */
  toTeamList(){
    wx.navigateTo({
      url: '/pages/team-list/team-list'
    });
  },

  /**
   * 跳转到寻找职位列表页面
   */
  toRecruitmentList(){
    wx.navigateTo({
      url: '/pages/recruitment-list/recruitment-list'
    });
  },

  /**
   * 跳转到活动列表页面
   */
  toActivityList(e){
    wx.navigateTo({
      url: '/pages/activity-list/activity-list?orderid=' + e.currentTarget.dataset.order
    });
  },

  /**
   * 跳转到活动详情页面
   */
  toActivityInfo(e){
    wx.navigateTo({
      url: '/pages/activity-info/activity-info?id=' + e.currentTarget.dataset.id
    });
  },

  /**
   * 待开发模块
   */
  showTip(){
    wx.showModal({
      title: '提示',
      content: '模块待开发！',
      showCancel: false
    })
  }
});
